package adminController

import (
	"gin/chibuda/app/admin/adminService"
	"gin/chibuda/app/utils"
	"time"
	//"gin/chibuda/app/admin/adminService"
	"gin/chibuda/system/ws"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	uuid "github.com/satori/go.uuid"
	"net/http"
)

func SystemInit(c *gin.Context){
	conn, err := (&websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}).Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		http.NotFound(c.Writer, c.Request)
		return
	}
	client := &ws.Client{
		Id:     uuid.NewV4().String(),
		Group:  "test",
		Socket: conn,
		Message:   make(chan []byte, 1024),
		State:true ,
	}
	go ws.WebsocketManager.Start()
	ws.WebsocketManager.RegisterClient(client)
	for {
		r := adminService.SystemInit()
		rs:=utils.Json_encode(r.Rs)
		go ws.WebsocketManager.Send(client.Id,client.Group,[]byte(rs))
		go ws.WebsocketManager.SendService()
		go client.Write()
		time.Sleep(time.Duration(1)*time.Second)
		if !client.State {
			break
		}
	}
}

func DictGet(c *gin.Context){
	data := utils.Input(c)
	r := adminService.DictGet(data)
	c.JSON( r.HttpCode, r.Rs)
}

func DictuEdit(c *gin.Context){
	data := utils.Input(c)
	r := adminService.DictuEdit(data)
	c.JSON( r.HttpCode, r.Rs)
}

func DictDel(c *gin.Context){
	data := utils.Input(c)
	r := adminService.DictDel(data)
	c.JSON( r.HttpCode, r.Rs)
}